package com.syp.auth.web.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.syp.auth.common.Constants;
import com.syp.auth.domain.vo.ResourceVo;
import com.syp.auth.domain.vo.RoleVo;
import com.syp.auth.domain.vo.easyui.Json;
import com.syp.auth.export.annotation.SecurityNone;
import com.syp.auth.export.common.GeneralUserVo;
import com.syp.auth.export.common.GorgeousParameters;
import com.syp.auth.export.common.TokenCooikeMaster;
import com.syp.auth.export.resource.AuthorizeResourceI;
import com.syp.auth.export.util.CurrentUserHolder;
import com.syp.auth.service.base.RoleServiceI;
import com.syp.auth.service.base.UserServiceI;

@Controller
@RequestMapping("/sys/role")
public class RoleController {
	
	private static final Logger log = LoggerFactory.getLogger(RoleController.class);

	@Autowired
	private RoleServiceI roleService = null;
	@Autowired
	private AuthorizeResourceI authorizeResource = null;
	@Autowired
	private UserServiceI userService;

	@RequestMapping("/main")
	public ModelAndView mainPage(@CookieValue(GorgeousParameters.AUTH_COOKIE_NAME) String sid) {
		ModelAndView mv = new ModelAndView("sys/role/role");
		GeneralUserVo guser = CurrentUserHolder.get();
		if (guser == null) {
			guser = this.authorizeResource.cuser(sid);
		}
		List<String> permitUrlList = guser.getPermitUrlList();
		mv.addObject("permitUrlList", permitUrlList);
		return mv;
	}

	/**
	 * 新增页面跳转action
	 * 
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return "sys/role/roleAddEdit";
	}

	/**
	 * 编辑页面跳转action
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage() {
		return "sys/role/roleAddEdit";
	}

	/**
	 * 查看页面跳转action
	 * 
	 * @return
	 */
	@RequestMapping("/showPage")
	public String showPage() {
		return "sys/role/roleShow";
	}

	@ResponseBody
	@RequestMapping("/add")
	public Json add(RoleVo roleVo) {
		Json json = new Json();
		try {
			this.roleService.add(roleVo);
			json.setSuccess(true);
			json.setMsg("添加成功！");
		} catch (Exception e) {
			log.error("添加角色异常。", e);
			json.setMsg("添加失败！");
		}

		return json;
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Json delete(Long id) {
		Json json = new Json();
		try {
			this.roleService.delete(id);
			json.setSuccess(true);
			json.setMsg("删除成功！");
		} catch (Exception e) {
			log.error("删除角色异常。", e);
			json.setMsg("删除失败！");
		}

		return json;
	}

	@ResponseBody
	@RequestMapping("/edit")
	public Json edit(RoleVo roleVo) {
		Json json = new Json();
		try {
			this.roleService.edit(roleVo);
			json.setSuccess(true);
			json.setMsg("编辑成功！");
		} catch (Exception e) {
			log.error("编辑角色异常。", e);
			json.setMsg("编辑失败！");
		}

		return json;
	}

	@ResponseBody
	@RequestMapping("/getObject")
	public RoleVo getObject(Long id) {
		RoleVo roleVo = roleService.get(id);
		return roleVo;
	}

	@RequestMapping("/grantPage")
	public String grantPage() {
		return "sys/role/roleGrant";
	}

	@ResponseBody
	@RequestMapping("/grant")
	public Json grant(RoleVo role, @CookieValue(GorgeousParameters.AUTH_COOKIE_NAME) String sid) {
		Json json = new Json();
		try {
			GeneralUserVo guser = CurrentUserHolder.get();
			if (guser == null) {
				guser = this.authorizeResource.cuser(sid);
			}
			List<Long> myResIdList = null;
			if (guser.getId() != Constants.SUPER_MAN_NO) {
				myResIdList = this.userService.myResIdList(guser.getId());
				for (ResourceVo r : guser.getPermitResList()) {
					myResIdList.add(r.getId());
				}
			}
			this.roleService.grant(role, myResIdList);
			json.setSuccess(true);
			json.setMsg("授权成功！");
		} catch (Exception e) {
			log.error("授权异常。", e);
			json.setMsg("授权失败！");
		}

		return json;
	}

	@ResponseBody
	@RequestMapping("/allRoleGrid")
	public Object allRoleGrid(RoleVo roleVo) {
		Object object = null;
		try {
			object = this.roleService.treegrid(roleVo, false).getChildren();
			if (object == null) {
				object = new Object();
			}
		} catch (Exception e) {
			log.error("获取角色树异常。", e);
			object = new Object();
		}

		return object;
	}

	@ResponseBody
	@RequestMapping("/myRoleTree")
	public Object myRoleTree(RoleVo root, Boolean recursive, HttpServletRequest request) {
		Object object = new Object();
		try {
			Cookie token = TokenCooikeMaster.getInstunce().get(request);
			if (token != null) {
				GeneralUserVo guser = CurrentUserHolder.get();
				if (guser == null) {
					guser = this.authorizeResource.cuser(token.getValue());
					//CurrentUserHolder.set(guser);
				}
				List<Long> myRoleIdList = null;
				if (guser.getId() != Constants.SUPER_MAN_NO) {
					myRoleIdList = this.userService.myRoleIdList(guser.getId());
				}
				recursive = recursive == null ? false : recursive;
				object = this.roleService.tree(root, myRoleIdList, recursive).getChildren();
			}
		} catch (Exception e) {
			log.error("获取角色树异常。", e);
		}
		return object;
	}

	@SecurityNone
	@ResponseBody
	@RequestMapping("/checkRoleCodeUnique")
	public Json checkRoleCodeUnique(String roleCode) {
		Json json = new Json();
		try {
			boolean unique = this.roleService.checkRoleCodeUnique(roleCode);
			json.setSuccess(unique);
			json.setMsg(unique ? "角色名称唯一!" : "角色名称已存在!");
		} catch (Exception e) {
			log.error("检验角色名称唯一性异常。", e);
		}

		return json;

	}
}
